/*********************************************************************************
 *      Copyright:  (C) 2022 lijiale<244971365@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  atcmd.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(19/01/22)
 *         Author:  lijiale <244871365@qq.com>
 *      ChangeLog:  1, Release initial version on "19/01/22 08:54:20"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>

#include "comport.h"

int send_atcmd(comport_t *comport,char *atcmd,char *buf,int buf_size,int timeout)
{
    int                 rv = -1;
    fd_set              rfds;
    struct timeval      time_out;
    struct timeval      *time;
    char                *ptr;
    int                 bytes = 0;

    if(!comport || !atcmd || !buf || buf_size <= 0)
    {
        printf("%s,invalid parameter...\n",__func__);
        return -1;
    }

    rv = comport_send(comport,atcmd,strlen(atcmd));
    if( rv < 0 )
    {
        printf("comport_send is failed:%s...\n",strerror(errno));
        return -2;
    }

    memset(buf,0,buf_size);

    FD_ZERO(&rfds);
    FD_SET(comport->fd,&rfds);

    time_out.tv_sec = 0;
    time_out.tv_usec = timeout * 1000;

    ptr = buf;

    while(1)
    {
        rv = select(comport->fd + 1, &rfds, NULL, NULL, &time_out);
        if( rv < 0 )
        {
            printf("select open failed:%s.\n ",strerror(errno));
            break;
        }
        else if( 0 == rv )
        {
            rv = -4;
            break;
        }
        else
        {
            rv = read( comport->fd, ptr, buf_size - bytes );
            if( rv < 0 )
            {
                printf("%s,read is failure.\n",__func__);
                return -5;
            }
            ptr += rv;
            bytes += rv;

            if( strstr(buf,"OK\r\n") )
            {
                rv = 0;
                break;
            }

            if( strstr(buf,"ERROR\r\n") )
            {
                rv = -6;
            }
        }
    }

    return rv;
}

int at_fetch(char *atreply, char *start_key, char *end_key, char *rbuf, int size)
{
    char                    *rbuf_start;
    char                    *rbuf_end;
    int                     rv =-1;

    if( !atreply || !start_key || size < 0 )
    {
        printf("%s,invalid parameter...\n",__func__);
        return -1;
    }

    memset(rbuf,0,size);

    rbuf_start = strstr(atreply,start_key);
    if( rbuf_start == NULL )
    {
        printf("strstr start_key :%s failure\n", start_key);
        return -2;
    }

    rbuf_start += strlen(start_key);

    if( end_key != NULL )
    {
        rbuf_end = strstr(rbuf_start,end_key);
        if( rbuf_end == NULL )
        {
            printf("strstr end_key %s failure\n", rbuf_end);
            return -3;
        }
    }

    if( rbuf != NULL && ((rbuf_end-rbuf_start) <= size) )
    {
        strncpy(rbuf, rbuf_start, rbuf_end-rbuf_start);
    }

    return 0;
}
